#include "../mqserver/consumer.hpp"
#include <gtest/gtest.h>

zhong::ConsumerManager::ptr manager;
class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        manager = std::make_shared<zhong::ConsumerManager>();
        manager->initQueueConsumer("queue1");
    }
    virtual void TearDown() override
    {
        manager->clear(); // 清理数据库
    }
};
void Callback(const std::string &tag, const zhong::BasicProperties *bp, const std::string &body)
{
    DEBUG_LOG("[%s]消费了消息[%s]", tag.c_str(), body.c_str());
}
TEST(consumer_test, create_test)
{
    manager->create("consumer1", "queue1", false, Callback);
    manager->create("consumer2", "queue1", false, Callback);
    manager->create("consumer3", "queue1", false, Callback);

    ASSERT_EQ(manager->exist("consumer1", "queue1"), true);
    ASSERT_EQ(manager->exist("consumer2", "queue1"), true);
    ASSERT_EQ(manager->exist("consumer3", "queue1"), true);
}

TEST(consumer_test, remove_test)
{
    manager->remove("consumer1", "queue1");

    ASSERT_EQ(manager->exist("consumer1", "queue1"), false);
    ASSERT_EQ(manager->exist("consumer2", "queue1"), true);
    ASSERT_EQ(manager->exist("consumer3", "queue1"), true);
}

TEST(consumer_test, choose_test)
{
    
    zhong::Consumer::ptr consumer = manager->choose("queue1");
    ASSERT_NE(consumer.get(), nullptr);
    ASSERT_EQ(consumer->tag, "consumer2"); // consumer1已删除

    consumer= manager->choose("queue1");
    ASSERT_NE(consumer.get(), nullptr);
    ASSERT_EQ(consumer->tag, "consumer3");
    // 轮转回2
    consumer= manager->choose("queue1");
    ASSERT_NE(consumer.get(), nullptr);
    ASSERT_EQ(consumer->tag, "consumer2");
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    RUN_ALL_TESTS();
    return 0;
}

